package com.qbw.app.module.user.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.qbw.app.Config;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.UserActivityLoginRegistBinding;
import com.qbw.app.model.Version;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.activity.DeveloperActivity;
import com.qbw.app.module.common.fragment.UpdateDialogFragment;
import com.qbw.app.request.impl.CommonRequest;

import pub.devrel.easypermissions.EasyPermissions;


/**
 * @author QBW
 * @date 2019/8/27
 */
public class UserLoginRegistActivity extends CommonActivity<UserActivityLoginRegistBinding> {

    public static void start(Context context) {
        Intent intent = new Intent(context, UserLoginRegistActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.user_activity_login_regist;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.tvLogin.setOnClickListener(this);
        mBinding.tvLoginByThird.setOnClickListener(this);
        mBinding.tvRegist.setOnClickListener(this);
        //mBinding.tvDeveloper.setVisibility(Config.isDeveloperChannel() ? View.VISIBLE : View.GONE);
        mBinding.tvDeveloper.setOnClickListener(this);
        mGeneralManager.getRequestManager().getCommonRequest().getCountryList(null).execute();
        String[] perms = new String[]{
                Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this,
                                               getResources().getString(R.string.app_need_permission),
                                               0,
                                               perms);
        }
//        if(!XApplication.getInstance().isCheckUpdate()) {
//            mGeneralManager.getRequestManager().getCommonRequest().checkUpdate(this).execute();
//        }
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_login) {
            UserLoginActivity.start(this);
        } else if (vid == R.id.tv_login_by_third) {

        } else if (vid == R.id.tv_regist) {
            UserRegistActivity.start(this);
        } else if (vid == R.id.tv_developer) {
            DeveloperActivity.start(this);
        }
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(CommonRequest.METHOD_CHECK_UPDATE)) {
            Version.Response res = (Version.Response) entity;
            Version version = res.getData();
            if (version != null && version.hasUpdate()) {
                XApplication.getInstance().setCheckUpdate(true);
                UpdateDialogFragment.createInstance(version)
                        .show(getSupportFragmentManager(), "update");
            }
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
    }
}
